package ultweb.web;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ultweb.domain.Prodotto;
import ultweb.service.ProductManager;
import ultweb.service.Aggiorna;
import ultweb.service.Registrazione;


public class AggiornaController extends SimpleFormController /*implements Controller*/ {

    protected final Log logger = LogFactory.getLog(getClass());
    private String me="AggiornaController-";
    private ProductManager productManager;
    private Map<String, Object> myModel;
  
    
    public AggiornaController(){
		setCommandClass(Aggiorna.class);
		setCommandName("aggiorna");
	}
 
    
    public ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response, Object command, BindException errors)throws ServletException {
        String foo=me +"onSubmit- ";
        logger.info(foo+"begin ");
        
        Aggiorna aggiornaProdotto=(Aggiorna) command;
        
        String marca=aggiornaProdotto.getMarca();
        Integer id=aggiornaProdotto.getId();
        
        if (marca!= null && !marca.equalsIgnoreCase("")){
        	productManager.aggiornaProdotto(productManager.getProdottoByID(id));
        }
        logger.info(foo+"Marca = " +marca);
        logger.info(foo+"end ");
       
       
//        return new ModelAndView("aggiornaSucess","aggiorna",aggiornaProdotto);
        return new ModelAndView(new RedirectView(getSuccessView()));
       
    }
  
    protected Object formBackingObject(HttpServletRequest request) throws ServletException {
    	String foo=me +"formBackingObject- ";
    	logger.info(foo+"begin "+request);
    	Aggiorna aggP = new Aggiorna();
    	//aggP.setMarca("pippo");
    	//aggP.setId(new Integer(0)); 
    	logger.info(foo+"end");
    	return aggP;
    }
    

   protected Map referenceData(HttpServletRequest request)throws Exception{
    	String foo=me +"referenceData- ";
    	myModel= new HashMap<String, Object>();;
    	logger.info(foo+"begin ");
    	myModel.put("prodotti", this.productManager.getProdotti());
    	return myModel;
    }
    
    public void setProductManager(ProductManager productManager) {
        this.productManager = productManager;
    }
    
    public ProductManager getProductManager() {
        return productManager;
    }
    
    
   protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors){
	   String foo=me +"showForm- ";
		logger.info(foo+"begin ");
		myModel = new HashMap<String, Object>();
	    myModel.put("prodotti", this.productManager.getProdotti());
	    logger.info(foo+"end");     
	    
	    return new ModelAndView("aggiorna", "model", myModel);
	}  

}